package com.fxwater.admin.controller.biz;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fxwater.biz.domain.FxwAlermConfigInfo;
import com.fxwater.biz.domain.PumpStationInfo;
import com.fxwater.biz.domain.UnitInfo;
import com.fxwater.biz.service.IUnitService;
import com.fxwater.common.annotation.Log;
import com.fxwater.common.core.controller.BaseController;
import com.fxwater.common.core.domain.AjaxResult;
import com.fxwater.common.core.page.TableDataInfo;
import com.fxwater.common.enums.BusinessType;
import com.fxwater.framework.util.ShiroUtils;

/**
 * 泵站单位管理
 * 
 * @author tx
 *
 */
@Controller
@RequestMapping("/biz/unit")
public class UnitController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(UnitController.class);

	private String prefix = "biz/unit";

	@Autowired
	private IUnitService unitService;

	
	/**
	 * 修改息状态
	 * @return
	 */
	@RequestMapping("/changeStatus")
	@ResponseBody
	@RequiresPermissions("biz:unit:edit")
	public AjaxResult changeStatus(String id,String status) {
		UnitInfo unitInfo = new UnitInfo();
		unitInfo.setId(Long.parseLong(id));
		unitInfo.setIsValid(status);
		return toAjax(unitService.editSaveUnitInfo(unitInfo));
	}
	
	/**
	 * 跳转页面
	 * 
	 * @return
	 */
	@RequiresPermissions("biz:unit:view")
	@GetMapping()
	public String pumpStation() {
		return prefix + "/unit";
	}

	@RequiresPermissions("biz:unit:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(UnitInfo unitInfo) {
		startPage();
		List<UnitInfo> list = unitService.selectUnitInfoInfoList(unitInfo);
		return getDataTable(list);
	}

	@RequiresPermissions("biz:unit:list")
	@PostMapping("/list/{id}")
	@ResponseBody
	public TableDataInfo addList(@PathVariable("id") String stationId) {
		startPage();
		UnitInfo unitInfo = new UnitInfo();
		unitInfo.setPsId(Long.parseLong(stationId));
		List<UnitInfo> list = unitService.selectUnitInfoInfoList(unitInfo);
		return getDataTable(list);
	}

	/**
	 * 跳转修改页面
	 * 
	 * @param mmap
	 * @return
	 */
	@GetMapping("edit/{id}")
	public String edit(ModelMap mmap, @PathVariable("id") String id,String type) {
		UnitInfo unitInfo = new UnitInfo();
		unitInfo.setId(Long.parseLong(id));
		unitInfo = unitService.selectUnitInfoInfoList(unitInfo).get(0);
		mmap.addAttribute("unitInfo", unitInfo);
		if( !"1".equals(type) ) {
			return prefix + "/edit";
		}else {
			return prefix + "/detailEdit";
		}
		
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping("/remove")
	@ResponseBody
	@RequiresPermissions("biz:unit:remove")
	@Log(title = "管理单位删除", businessType = BusinessType.DELETE)
	public AjaxResult remove(String ids) {
		return toAjax(unitService.removeUnitInfo(ids));
	}

	/**
	 * 增加管理单位
	 * 
	 * @param mmap
	 * @return
	 */
	@GetMapping("/add")

	public String add(String stationId, String stationName, ModelMap mmap) {
		mmap.put("stationId", stationId);
		mmap.put("stationName", stationName);

		if (StringUtils.isEmpty(stationId)) {
			return prefix + "/add";
		} else {
			return "biz/pumpStation/addUnit";
		}

	}


	/**
	 * 添加泵站
	 * 
	 * @param user
	 * @return
	 */
	@RequiresPermissions("biz:unit:add")
	@Log(title = "管理单位", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(UnitInfo unitInfo) {
		unitInfo.setCreateBy(ShiroUtils.getLoginName());//
		return toAjax(unitService.addUnitInfo(unitInfo));
	}

	/**
	 * 保存泵站修改信息
	 * 
	 * @return
	 */
	@RequiresPermissions("biz:unit:edit")
	@Log(title = "泵站管理", businessType = BusinessType.UPDATE)
	@PostMapping("edit")
	@ResponseBody
	public AjaxResult editSave(UnitInfo unitInfo) {

		return toAjax(unitService.editSaveUnitInfo(unitInfo));
	}

}
